package yunquan.wang.base.web.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.data.domain.Page;

import yunquan.wang.base.config.AppConfig;
import yunquan.wang.modules.base.entity.SystemConfig;

public class PageTaglib extends TagSupport {

	private Page page;

	private String pageFunction;

	public String getPageFunction() {
		return pageFunction;
	}

	public void setPageFunction(String pageFunction) {
		this.pageFunction = pageFunction;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
 if(page!=null)
 {
	 

		StringBuffer sbf = new StringBuffer();
		//head
		sbf.append("<div  class=\"pagination\">");
		sbf.append("<div class=\"pageBar\">");
		sbf.append("每页");
		sbf.append("<select name=\"pageSize\" onchange=\"\"><option>10</option><option>20</option><option>30</option><option>50</option><option>100</option></select>");
		sbf.append("条记录");
		sbf.append(" 共有记录 "+page.getTotalPages()+"页/"+page.getTotalElements()+" 条,");
				sbf.append("当前显示 "
						+ (page.getNumber() * page.getSize() + 1)
						+ " 至 "
						+ (page.getNumber() * page.getSize() + page
								.getNumberOfElements()) + " 条 ")
				.append("</div><div class=\"rightObject\">");
	 //分页
				sbf.append("<ul>");
		if (!page.isFirstPage()) {
			sbf.append("<li");
			sbf.append( pageScript(1));
			sbf.append("><a class=\"button white\" href=\"#\">首页</a></li>");
		}
		if (page.hasPreviousPage()) {
			sbf.append("<li");
			sbf.append( pageScript(page.getNumber()));
			sbf.append("><a class=\"button white\" href=\"#\">上页</a></li>");
		}
		// TODO,这个算法有待改进
		int halfSize = AppConfig.DISPLAY_PAGE_NO / 2;
		int totalPage = page.getTotalPages();
		long startPageNumber = Math.max(page.getNumber() - halfSize, 1);
		long endPageNumber = Math.min(startPageNumber
				+ AppConfig.DISPLAY_PAGE_NO - 1, totalPage);
		if (endPageNumber - startPageNumber < AppConfig.DISPLAY_PAGE_NO) {
			startPageNumber = Math.max(endPageNumber
					- AppConfig.DISPLAY_PAGE_NO, 1);
		}
		for (Long i = startPageNumber; i <= endPageNumber; i++) {
			sbf.append("<li");
			if ((page.getNumber() + 1) == i) {
				sbf.append("><a class=\"button white active\" href=\"#\">");
			} else {
				sbf.append(pageScript(i.intValue()));
				sbf.append("><a class=\"button white\" href=\"#\">");
			}
			sbf.append(i);
			sbf.append("</a></li>");
		}
		
		if (page.hasNextPage()) {
			sbf.append("<li");
			sbf.append( pageScript(page.getNumber() + 2));
			sbf.append("><a class=\"button white\" href=\"#\">下页</a></li>");
		}
		if (!page.isLastPage()) {
			sbf.append("<li");
			sbf.append( pageScript(page.getTotalPages()));
			sbf.append("><a class=\"button white\" href=\"#\">末页</a></li>");
		}
		sbf.append("<li><input type=\"text\"  maxlength=\"8\" style=\"width:20px;\"></input></li>");
		sbf.append("<li><a class=\"button white\" href=\"#\">跳转</a></li>");
		sbf.append("</ul>");
		sbf.append("</div>");
		sbf.append("</div>");
		try {
			out.print(sbf.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
 }
		return EVAL_PAGE;
	}

	private String pageScript(Integer pageNo) {
		return " onclick=\"" + pageFunction + "(" + pageNo + ");\"";
	}
}
